package cn.zch.bank.abchina;
import com.abchina.openbank.OpenBankHttpClient;
import com.abchina.openbank.OpenBankHttpRequest;
import com.abchina.openbank.common.Contants;

import java.util.HashMap;
import java.util.Map;
public class BankAbchinaDemo {
  //第一个是合作方自己的证书；第二个是合作方自己的证书密码；第三个是农行的公钥证书
  //    private static final String pfxFile="D:/pfxFile/24501a5d-e841-4827-b2b3-d14a9ea3fba4.pfx";
  private static final String pfxFile = "E:\\bankabc\\download\\ABC_OpenBank_ThridPart_Test.pfx";
  private static final String pfxPwd = "111111";
  //    private static final String cerFile="D:/pfxFile/dev.cer";
  private static final String cerFile = "E:\\bankabc\\download\\ABC_Openbank_Sandbox.cer";
  //应用编号赋值
  //  private static final String appId = "24501a5d-e841-4827-b2b3-d14a9ea3fba4";
  private static final String appId = "ef62c1a4-91cf-4f47-a0d1-741586255dd6";
  //AES对称加密的密钥
  //  private static final String appSecret = "cff054d1d7c84036a3dc160f9457e5f423704b15";
  private static final String appSecret = "b7468d7dd2924a65a21099b14f0b512ff6d44374";
  /**
   * private static final String baseUrl0 = "";
   */
  private static final String baseUrl1 = "http://10.235.237.141:9081";
  public static void main(String[] args) throws Exception {
    //初始化开放银行请求参数（首次运行时调用）
    OpenBankHttpClient.initOpenBankHttpClient(appId, pfxFile, pfxPwd, cerFile, appSecret);
    OpenBankHttpRequest request = creatRequest();
    //生成请求报文
    //对于API接口,生成请求报文后,还需要调用下一步SendAndRecv;
    request.generateRequestString();
    //request.getRequestString(); //用于h5页面的form表单
    //发送请求并接收响应报文--仅业务报文,不包含公共字段,并已经经过验签和解密:
    //设置请求超时时间（可选）
    request.setTimeout(30000);
    String responseTxt = request.sendAndRecv();
  }
  //创建请求信息
  private static OpenBankHttpRequest creatRequest() throws Exception {
    OpenBankHttpRequest request = new OpenBankHttpRequest();
    //签名算法赋值,目前支持SHA256和HASHANDSHA256两种.第一种是对报文明文进行加签,一般采用这种方法,第二种是针对报文明文的哈希值进行加签;
    //报文中较长时,如含有图片信息,推荐采用"HASHANDSHA256";
    request.setSignType(Contants.SHA256);
    //业务报文赋值-根据业务报文的格式任选一种
    int type = 1;
    if (type == 1) {
      //业务报文-普通的Json字符串
      String bizData = "{\"Code\":\"1111\"}";
      request.setBizData(bizData);
      request.setRequestUrl(baseUrl1 + "/AraratGateWay/openabc/api/h5eaccount/queryresult/v1");
    }
    if (type == 2) {
      //业务报文-示例2--业务报文类似{"data":{"":""}}
      Map<String, Object> data = new HashMap<>();
      Map<String, Object> map = new HashMap<>();
      map.put("codCity", "110000");
      map.put("levelOne", "1100");
      map.put("channel", "MBNK");
      data.put("data", map);
      request.setBizData(data);
      request.setRequestUrl(baseUrl1 + "/AraratGateWay/openabc/api/sociallifepayment/gettypes/v1");
    }
    if (type == 3) {
      //业务报文-示例3--业务报文类似{"VALUE":""}
      Map<String, Object> map = new HashMap<>();
      map.put("VALUE", "{\"Content\":{\"Message\": {\"Version\": \"V3.0.0\", \"Format\": \"JSON\",\"Merchant\": {\"ECMerchantType\": \"EBUS\",\"MerchantID\": \"103881104410001\"}, \"TrxRequest\": {\"MerchantNo\":\"103881104410001\",\"TrxType\":\"QueryPayType\"}}},\"TransCode\":\"QueryPayType\"}");
      request.setBizData(map);
      request.setRequestUrl(baseUrl1 + "/AraratGateWay/openabc/api/sociallifepayment/findtranstype/v1");
    }
    if (type == 4) {
      //业务报文-示例4--请求参数在header中.
      Map<String, Object> header = request.getHeader();
      header.put("Authorization", "bearer AAEAAEaZhnQajHeCPUY9y3qlmq1CgM8wSkQl-eFKMQGh96wfHpZpce21NiGzflDEi7jkpqFtzfEBHiNgqcukq0uIw4V8BJjnN1Q3dRYGH1w2kZ2IFH7c4RvvisqKgeoZWRKS7OsFopwAWP3uOlgnE_BsGdZAY99J1quIUqh-NYi06R7G8mAdJ0xge8UsfCuPqR0P0vOZmDEeMQh91zmW3dBtG5GWTJ8cmSDWBOLfbl-bVea46P0UzemjenXjFblYpCiFEcI0rZOvgsXRbKArl1AZ5093rNuStaxnnlIIQ36z2tejawc5epMbA-R5ljRZE5U4bHGbw5fHVZMTGlMfZqPpNAjUAQAAAAEAABq1bZsaLjt2o93d2lqS4JAlZern9RDz77oAaON1QnE3cNZP8EzUj0qTHc-AeJT7UjSLwR5BlNAcpaE_K_F6BN1Nu_oHb9jyui6zHXXQeOdmKQN2aNLNSJ9bmQCMYTKTKaGbpzITKiJkhTLBnBLxme_X-0KgY4yZc7GLc0E9Jq6YINhK_I-AP_NsGgDuHHjS0wqlzEVfIeyZtyKeG97Y4ol0q5FsgaJrihAco2kyGRbs9VUVL9f7fDSMpNJK_dtUm0r3f5zZHID7WkWpR6g_OSQdiYuhBXXWqLb8uMiRbpbgtnbrJuaPQHhyjE6V4xazYBbgjjbCi8h32TPb-AqizM4Nd77burWxsUs2KEQYWySN3Mh5NPl-2J9zu9j9HS00mQ2pddB_ljuAGQrlPHmsyeAGvWLrB5ZfWKWAfi2hQiNExG0-QskWgAVJSGnEFnYkbiLUh40cqoNa1zsONEJTYLapJlj9h-1UyIbV1lss3pHE8NK_0QNMkty0eBoiIjcmcRyhOzQpJ35hPbEv8TOcGlw6Ib20nwluPjeERgyg91qn_AnACH2vo8Dz3ufQLQpAB2-DknbcgJdFJnJyfZXcbW6TXaWwCygLCqe48Zqqaojq");
      request.setBizData("");
      request.setRequestUrl(baseUrl1 + "/AraratGateWay/openabc/api/ket/userinfo/v1");
    }
    return request;
  }
}
